package filter_repeat

/**
通过滑动窗口，过滤重复数据
超过滑动窗口大小的不能判定重复
 */

type Filter struct {
	Content map[string]*Node
	Length int
	Head *Node
	Tail *Node
}

// 存储量滑动窗口大小
var limit int = 10000

func NewFilter() *Filter {
	con := make(map[string]*Node)
	return &Filter{
		Content: con,
	}
}

func (f *Filter) Deal(val string) bool {
	if _, ok :=  f.Content[val]; ok {
		return false
	}

	if f.Length == 0 {
		node1 := &Node{
			Front: nil,
			Behind: nil,
			Value: val,
		}
		f.Head = node1
		f.Content[val] = node1
		f.Length++
		return true
	}

	if f.Length == 1 {
		node2 := &Node{
			Front: f.Head,
			Behind: nil,
			Value: val,
		}
		f.Tail = node2
		f.Head.Behind = node2
		f.Content[val] = node2
		f.Length++
		return true
	}

	if f.Length < limit {
		node3 := &Node{
			Front: f.Tail,
			Behind: nil,
			Value: val,
		}
		f.Tail.Behind = node3
		f.Tail = node3
		f.Content[val] = node3
		f.Length++
	} else {
		node4 := &Node{
			Front: f.Tail,
			Behind: nil,
			Value: val,
		}
		f.Tail.Behind = node4
		f.Tail = node4
		f.Content[val] = node4
		tmp := f.Head
		f.Head = f.Head.Behind
		f.Head.Front = nil
		//tmp.Behind = nil
		//tmp.Front = nil
		delete(f.Content, tmp.Value)
		tmp = nil
		//fmt.Println(tmp.Value)
	}
	return true
}

